<?php
/*
 * Created on Sep 4, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php
class Model_type extends Model {
	var $_tbl_name;

	function Model_type() {
		// Call the Model constructor
		parent :: Model();
		$this->_tbl_name = "tbl_type";
	}
	
	/**
	 *
	 */
	function fetchAll(){
		$query = $this->db->get($this->_tbl_name);
		//echo $this->db->last_query();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return null;
	}
	/**
	 *
	 *
	 */
	function find($id){
		$this->db->where('id', $id);
		$query = $this->db->get($this->_tbl_name);	

		if ($query->num_rows() > 0) {
			return $query->first_row('array');
		}
		return null;
	}
	
	function insert_type($data){
		$sort = $this->get_max_sort();
		$data['sort'] = $sort + 1;
		$this->db->insert($this->_tbl_name, $data);
		$id = $this->db->insert_id();
		return $id;
	}
	
	
	/**
	 *
	 *
	 */
	function update_type($id, $data){
		$this->db->where('id', $id);
		$data['modified_date'] = date("Y-m-d h:i:s");
		return $this->db->update($this->_tbl_name, $data);
	}
	
	/**
	 *
	 *
	 */
	function get_max_sort()
	{
		$this->db->select_max('sort');
		$query = $this->db->get($this->_tbl_name);
		if ($query->num_rows() > 0) {
			$first_row = $query->first_row('array');
			return $first_row['sort'];
		}
		return 0;
	}
	
	/**
	 *
	 *
	 */
	function fetchByStoreId($storeId){
		$this->db->where('store_id', $storeId);
		$query = $this->db->get('tbl_store_type');	

		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return null;
	}
	/*
	 * 
	 * 
	 */
	function deleteByStoreId($storeId){
		$this->db->where('store_id', $storeId);
		return $this->db->delete('tbl_store_type');
	}
	
	/**
	 * 
	 * @return 
	 */
	function insertStoreType($data){
		$this->db->insert('tbl_store_type', $data);
		$id = $this->db->insert_id();
		return $id;
	}
}
?>